﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iLangl.Model
{
	public class Company : DuplexObject, ICompany
	{
		public String Name { get; set; }
		public String Address { get; set; }
		public String City { get; set; }
		public String State { get; set; }
		public String Zip { get; set; }
		public String Country { get; set; }
		public String Phone { get; set; }
		public String Currency { get; set; }

		private BillingInfo _billingInfo;
		public BillingInfo BillingInfo 
		{
			get
			{
				return _billingInfo;
			}
			set
			{
				_billingInfo = value;

				if (_billingInfo != null)
				{
					if(_billingInfo.Company != this)
						_billingInfo.Company = this;
				}
			}
		}

		private Contact[] contacts;
		public Contact[] Contacts {
			get
			{
				return contacts;
			}
			set
			{
				contacts = value;
				updateContacts();
			}
		}

		public void AddContact(Contact contact)
		{
			if (Contacts == null)
			{
				Contacts = new Contact[] { contact };
			}
			else
			{
				List<Contact> contacts = new List<Contact>();
				contacts.AddRange(Contacts);
				contacts.Add(contact);
				Contacts = contacts.ToArray();
			}
		}

		private void updateContacts()
		{ 
			if(Contacts == null)
			{
				return ;
			}
			foreach (Contact contact in Contacts)
			{
				contact.Company = this;
			}
		}
	}
}
